package com.hx.er.action.admin.food;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.detail.FoodTemplateDetailBean;
import com.hx.er.common.exception.ErException;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.util.FileUtil;
import com.hx.er.service.admin.IAdminFoodService;

public class AdminFoodTemplateDeleteAction extends ErAdminAction {

	// 序列化ID
	private static final long serialVersionUID = -5885896269081800347L;

	private long foodTemplateId;
	private FoodTemplateDetailBean foodTemplateDetailBean;

	// 多语言标签名
	private String labelLang1;
	private String labelLang2;
	private String labelLang3;
	private String labelLang4;
	private String labelLang5;

	@Autowired
	private IAdminFoodService adminFoodService;

	public String init() throws IOException {
		// 初始化页面
		this.initPage();

		return SUCCESS;
	}

	public String delete() throws IOException {
		if (this.foodTemplateDetailBean == null) {
			this.foodTemplateDetailBean = new FoodTemplateDetailBean();
		}
		
		this.foodTemplateDetailBean.setFoodTemplateId(this.foodTemplateId);
		this.foodTemplateDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		int count = this.adminFoodService.deleteFoodTemplate(this.foodTemplateDetailBean);

		if (count == 0) {
			// 初始化页面
			this.initPage();
			// 添加错误信息
			this.addActionError(super.getText(DELETE_FAILURE_MSG));
			return INPUT;
		} else {
			// 添加成功信息
			this.addActionMessage(super.getText(DELETE_SUCCESS_MSG));
			return SUCCESS;
		}
	}

	/**
	 * 检查文件，如果不存在则重新生成
	 * 
	 * @throws ErException
	 * @throws IOException
	 */
	private void checkPictureFile() throws IOException {
		// 如果食品对象不存在，则掠过该方法
		if (this.foodTemplateDetailBean == null) {
			return;
		}

		// 声明输出流
		OutputStream fos = null;

		// 循环处理列表中所有对象
		String root = request.getServletContext().getRealPath("/");
		File pictureFile = new File(root, this.foodTemplateDetailBean.getPicturePath());

		// 如果所在目录不存在，则先创建
		FileUtil.createDir(pictureFile);

		// 如果文件不存在，则写文件
		if (!pictureFile.exists()) {
			fos = new FileOutputStream(pictureFile);
			fos.write(this.foodTemplateDetailBean.getPictureFile());
			if (fos != null) {
				fos.close();
			}
		}
	}

	private void initPage() throws IOException {
		// 查询食品详细信息
		this.foodTemplateDetailBean = this.adminFoodService.getFoodTemplateAliveById(this.foodTemplateId);

		// 语言
		if (ErLanguages.getLangList().size() > 0) {
			this.labelLang1 = ErLanguages.getLocale(ErLanguages.getLangList().get(0).getLangId()).getLanguage().toUpperCase();
		}
		if (ErLanguages.getLangList().size() > 1) {
			this.labelLang2 = ErLanguages.getLocale(ErLanguages.getLangList().get(1).getLangId()).getLanguage().toUpperCase();
		}
		if (ErLanguages.getLangList().size() > 2) {
			this.labelLang3 = ErLanguages.getLocale(ErLanguages.getLangList().get(2).getLangId()).getLanguage().toUpperCase();
		}
		if (ErLanguages.getLangList().size() > 3) {
			this.labelLang4 = ErLanguages.getLocale(ErLanguages.getLangList().get(3).getLangId()).getLanguage().toUpperCase();
		}
		if (ErLanguages.getLangList().size() > 4) {
			this.labelLang5 = ErLanguages.getLocale(ErLanguages.getLangList().get(4).getLangId()).getLanguage().toUpperCase();
		}

		// 检查图片
		this.checkPictureFile();
	}

	public long getFoodTemplateId() {
		return foodTemplateId;
	}

	public void setFoodTemplateId(long foodTemplateId) {
		this.foodTemplateId = foodTemplateId;
	}

	public FoodTemplateDetailBean getFoodTemplateDetailBean() {
		return foodTemplateDetailBean;
	}

	public void setFoodTemplateDetailBean(FoodTemplateDetailBean foodTemplateDetailBean) {
		this.foodTemplateDetailBean = foodTemplateDetailBean;
	}
	
	public String getLabelLang1() {
		return labelLang1;
	}

	public void setLabelLang1(String labelLang1) {
		this.labelLang1 = labelLang1;
	}

	public String getLabelLang2() {
		return labelLang2;
	}

	public void setLabelLang2(String labelLang2) {
		this.labelLang2 = labelLang2;
	}

	public String getLabelLang3() {
		return labelLang3;
	}

	public void setLabelLang3(String labelLang3) {
		this.labelLang3 = labelLang3;
	}

	public String getLabelLang4() {
		return labelLang4;
	}

	public void setLabelLang4(String labelLang4) {
		this.labelLang4 = labelLang4;
	}

	public String getLabelLang5() {
		return labelLang5;
	}

	public void setLabelLang5(String labelLang5) {
		this.labelLang5 = labelLang5;
	}
}
